#include <Arduino.h>
#include <TFT_eSPI.h>
#include "MyFont.h"
#include "Astronaut.h"
#include "Weather.h"
#include "MyPic.h"
#include "Sharingan.h"

#define TFT_SCREEN_W 128
#define TFT_SCREEN_H 128

// put function declarations here:
TFT_eSPI tft = TFT_eSPI();
/*
    x indicate x coordinate, y indicate y coordinate, 
    font indicate show text, can chinese or english,
    color indicate test color, type indicate the type of text, 1 - english 2 - chinese
*/
void showFont(int x, int y, const char *font, uint16_t color, int type);
void showPic(int x, int y, int w, int h, const uint16_t* pic);

void setup()
{
    // put your setup code here, to run once:
    Serial.begin(115200);
    // Serial.printf("hello\n");
    tft.init();
    tft.fillScreen(TFT_BLACK);
    // showFont(185, 10, "周日", TFT_WHITE, 2);
    // showFont(1000, 50, "abc123456", TFT_WHITE, 1);
    // showPic(0, 50, 64, 64, Weather[0]);
    // showPic(0, 0, 128, 128, MyPic[0]);
}
int i = 0;
void loop()
{
    // showPic(64, 50, 64, 64, Astronaut[i]);
    // delay(100); //延时
    // i += 1; //下一帧
    // if (i>8) { i=0; }

    showPic(0, 0, 120, 120, Sharingan[i]);
    delay(100); //延时
    i += 1; //下一帧
    if (i>11) { i=0; } 
}

// put function definitions here:
void showFont(int x, int y, const char *font, uint16_t color, int type)
{
    x %= TFT_SCREEN_W;
    y %= TFT_SCREEN_H;
    switch (type)
    {
    case 1:
        tft.setCursor(x, y, 1);
        tft.setTextColor(color);
        tft.setTextSize(2);
        tft.println(font);
        break;
    case 2:
        int font_num = strlen(font) / 3;
        Serial.printf("字数 :%d \n", font_num);
        for (int i = 0; i < font_num; i++)
        {
            for (int j = 0; j < FONT_NUM; j++)
            {
                if (strncmp(hanzi[j].hz, font, 3) == 0)
                {
                    tft.drawBitmap(x, y, hanzi[j].hz_Id, 16, 16, color);
                    break;
                }
            }
            font += 3;
            x += 17;
        }
        break;
    }
}

void showPic(int x, int y, int w, int h, const uint16_t* pic)
{
    tft.setSwapBytes(true);  //使图片颜色由RGB->BGR
    tft.pushImage(x, y, w, h, pic);
}